import {Component, OnInit, ElementRef, ViewChild, AfterViewInit} from '@angular/core';
import * as _ from 'lodash';
import {FetchService} from "../../../../../core/net/fetch.service";
import * as moment from "moment";
import { UniversalService } from '../../../../../core/util/universal.service';
import { Router } from '@angular/router';
import { DispatchPlanService } from '../../../dispatch-plan.service';

@Component({
    selector: 'app-slt-dispatch-plan-month',
    templateUrl: './slt-dispatch-plan-month.component.html',
    styleUrls: ['./slt-dispatch-plan-month.component.scss']
})
export class SltDispatchPlanMonthComponent implements OnInit, AfterViewInit {
    year = "";    // 默认今年
    manageId: string;
    monthSelection = '';   //  4-9 d:冬灌
    monthPlanList = [];
    pageNum = 1;
    pageSize = 10;
    total: number;
    loading = true;
    tableHeight = "400px";
    scrollConfig =  {
       y: '400px'
    };
    @ViewChild('wrap') elementView: ElementRef;
    constructor(
        private fetchService: FetchService,
        private universalService: UniversalService,
        private router: Router,
        private dispatchService: DispatchPlanService,
    ) {
    }
    ngOnInit() {
        const userInfo = this.universalService.getUserProfile() || {};
        this.manageId = userInfo["manageId"];
        this.getGridData();
    }
    ngAfterViewInit() {
        this.scrollConfig.y = (this.elementView.nativeElement.offsetHeight - 160) + 'px';
    }
     // 月旬用水计划
    getGridData(reset: boolean = false): void {
        if (reset) {
            this.pageNum = 1;
        }
        const params = {
            pageNum: this.pageNum,
            pageSize: this.pageSize,
            reportId: 5,
            cycle: this.year ? moment(this.year).year() : "",
            m: this.monthSelection,
            manageId: this.manageId,
            orderByField: "state,cycle desc ,",
            orderBy: " m  desc,data_type  desc"
        };
        this.fetchService.post<any>('plan/wrsSchedulePlan/list', params).subscribe(res => {
            this.loading = false;
            if (Array.isArray(res.list)) {
                this.total = res['total'];
                this.monthPlanList = _.cloneDeep(res.list);
            } else {
                this.total = 0;
                this.monthPlanList = [];
            }
        });
    }

    onEdit(pageType, data): void {
        const paramsData = {
            y: data.cycle,
            m: data.m,
            dataType: data.dataType,
        };
        this.router.navigate(['/dispatch-plan/slt-dispatch-detail'], {
            skipLocationChange: true,
            queryParams: { pageType, paramsData: JSON.stringify(paramsData), planName: data.name, state: data.state}
        });
    }

    exportFile(data): void {
        const params = {
            reportId: '5',
            y: data.cycle,
            m: data.m,
            dataType: data.dataType,
        };
        this.dispatchService.exportExcel(params, data.name);
    }
    reset() {
        this.year = '';
        this.monthSelection = "";
        this.getGridData(true);
    }
}
